﻿/*********************************************************************************
**                                                                              **
**  Copyright (C) 2024 LiLong                                                   **
**  This file is part of GnssLogs.                                              **
**                                                                              **
**  GnssLogs is free software: you can redistribute it and/or modify            **
**  it under the terms of the GNU Lesser General Public License as published by **
**  the Free Software Foundation, either version 3 of the License, or           **
**  (at your option) any later version.                                         **
**                                                                              **
**  GnssLogs is distributed in the hope that it will be useful,                 **
**  but WITHOUT ANY WARRANTY; without even the implied warranty of              **
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               **
**  GNU Lesser General Public License for more details.                         **
**                                                                              **
**  You should have received a copy of the GNU Lesser General Public License    **
**  along with GnssLogs.  If not, see <https://www.gnu.org/licenses/>.          **
**********************************************************************************/
#pragma once

#include "../AVEPOS.h"
#include "LogHeaderDecoder.h"

namespace GnssLogs
{
namespace AVEPOSDecoder
{
using namespace tao::pegtl;
using Sync   = TAO_PEGTL_STRING("#AVEPOSA");
using Header = LogHeaderDecoder::SyncExpression<Sync>;
// clang-format off
struct Lat : Floating {};
struct Lon : Floating {};
struct Hgt : Floating {};
struct LatSigma : Floating {};
struct LonSigma : Floating {};
struct HgtSigma : Floating {};
struct PosAve : ascii::identifier {};
struct AveTime : star<digit> {};
struct Samples : star<digit> {};
// clang-format on
using Expression = seq<Header,
                       Lat,
                       Split,
                       Lon,
                       Split,
                       Hgt,
                       Split,
                       LatSigma,
                       Split,
                       LonSigma,
                       Split,
                       HgtSigma,
                       Split,
                       PosAve,
                       Split,
                       AveTime,
                       Split,
                       Samples,
                       one<'*'>,
                       CRC32,
                       Tail>;

template<typename T>
struct Action
{
};

template<>
struct Action<Header> : LogHeaderDecoder::LogHeaderHelperNoSetValue
{
};

template<>
struct Action<Lat> : DoubleHelper<Action<Lat>>
{
    static void setValue(AVEPOS::Data& data, double value) { data.lat = value; }
};

template<>
struct Action<Lon> : DoubleHelper<Action<Lon>>
{
    static void setValue(AVEPOS::Data& data, double value) { data.lon = value; }
};

template<>
struct Action<Hgt> : DoubleHelper<Action<Hgt>>
{
    static void setValue(AVEPOS::Data& data, double value) { data.hgt = value; }
};

template<>
struct Action<LatSigma> : FloatHelper<Action<LatSigma>>
{
    static void setValue(AVEPOS::Data& data, float value) { data.latSigma = value; }
};

template<>
struct Action<LonSigma> : FloatHelper<Action<LonSigma>>
{
    static void setValue(AVEPOS::Data& data, float value) { data.lonSigma = value; }
};

template<>
struct Action<HgtSigma> : FloatHelper<Action<HgtSigma>>
{
    static void setValue(AVEPOS::Data& data, float value) { data.hgtSigma = value; }
};

template<>
struct Action<PosAve> : EnumHelper<Action<PosAve>, PositionAveragingStatus>
{
    static void setValue(AVEPOS::Data& data, PositionAveragingStatus value) { data.posAve = value; }
};

template<>
struct Action<AveTime> : IntHelper<Action<AveTime>, uint32_t>
{
    static void setValue(AVEPOS::Data& data, uint32_t value) { data.aveTime = value; }
};

template<>
struct Action<Samples> : IntHelper<Action<Samples>, uint32_t>
{
    static void setValue(AVEPOS::Data& data, uint32_t value) { data.samples = value; }
};

template<>
struct Action<CRC32> : CRC32Helper<Action<CRC32>>
{
    static void setValue(AVEPOS::Data& data, uint32_t value) { data.crc32 = value; }
};
} // namespace AVEPOSDecoder
} // namespace GnssLogs